search.parse();
var form = new Form("form");

//文件上传控件
var wxverfileselect = $("#wxverfileselect");
var wxverfile = search.get("wxverfile");

//添加search-text点击弹出文件选择/上传控件变换事件
function setFileControlClick(){
	//text控件双击
	$(wxverfile._textboxhtml[0]).css("cursor","pointer");
	$(wxverfile._textboxhtml[0]).dblclick(function(){
		wxverfileselect.click();
	});
	//添加上传控件变换事件
	wxverfileselect.change(filechange);
}

$(function(){
	setFileControlClick();
	
    function tabs(tabTit,on,tabCon){
        $(tabTit).children().click(function(){
            $(this).addClass(on).siblings().removeClass(on);
            var index = $(tabTit).children().index(this);
           	$(tabCon).children().eq(index).show().siblings().hide();
    	});
	};
    tabs(".tab-hd","active",".tab-bd");
    $("body").find("ul[class=\"tab-hd\"]").children()[0].click();
    loadData();
});

function loadData(){
	pcGlobal.sendRequest(__ctx + "/system/getthesystemconfigs.xhtml", {}, function(data, status) {
		if (data.status) {
			if(data.result){
				form.setData(data.result);
			}
		} else {
			search.error({content: data.result});
		}
	});
}

function saveData(){
	if (!form.validate()) {
		return;
	}
	
	if(wxverfileselect[0].files[0]){
		//先上传文件
		var formData = new FormData();
		if(wxverfileselect[0].files[0]){
			formData.append("file", wxverfileselect[0].files[0]);
		}
		$.ajax({
			url : __ctx + "/fileupload/uploadthesystemfiles.xhtml",
			type : 'POST',
			data : formData,
			contentType:false,
			processData:false,
			success : function(result){
				//清空上传
				wxverfileselect[0].value = '';
				
				if (result.status) {
					//再保存表单
					var fdata = form.getData();
					pcGlobal.sendRequest(__ctx + "/system/updatesystemconfigs.xhtml", fdata, function(data, status) {
						if (data.status) {
							if(data.result){
								search.info({content: data.result.message});
							}
						} else {
							search.error({content: data.result});
						}
					});
				}else {
					search.error({content: result.result});
				}
			}
		});
	}else{
		var fdata = form.getData();
		pcGlobal.sendRequest(__ctx + "/system/updatesystemconfigs.xhtml", fdata, function(data, status) {
			if (data.status) {
				if(data.result){
					search.info({content: data.result.message});
				}
			} else {
				search.error({content: data.result});
			}
		});
	}
}

//文件上传变化事件
function filechange(e){
	var file = e.currentTarget.files[0];
	var filecontrolid = e.currentTarget.id;
	if(filecontrolid == "wxverfileselect"){
		if (!/^.+\.(txt)$/.test(file.name)) {
			search.warn({content : "不支持的文件类型！"});
			return;
		}
		wxverfile.setValue(file.name);
	}
}

